/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;

import com.anwen.mongo.toolkit.StringUtils;
import com.comac.ins.common.core.domain.model.LoginUser;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.satoken.utils.LoginHelper;
import com.comac.ins.isfd.constant.enums.IsfdImportMethodEnum;
import com.comac.ins.isfd.constant.enums.IsfdMongoQueryLogicalOperatorEnum;
import com.comac.ins.isfd.constant.enums.IsfdMongoQueryOperatorEnum;
import com.comac.ins.isfd.domain.*;
import com.comac.ins.isfd.domain.bo.*;
import com.comac.ins.isfd.domain.bo.query.IsfdServerDataQueryBo;
import com.comac.ins.isfd.domain.bo.query.IsfdServerManagementQueryBo;
import com.comac.ins.isfd.domain.vo.*;
import com.comac.ins.isfd.mapper.IsfdBusinessScenariosMapper;
import com.comac.ins.isfd.mapper.IsfdDatabaseModelConfigMapper;
import com.comac.ins.isfd.mapper.IsfdDatabaseTypeManagementMapper;
import com.comac.ins.isfd.mapper.IsfdServerManagementMapper;
import com.comac.ins.isfd.service.IIsfdDatabaseDataImportRecordService;
import com.comac.ins.isfd.service.IIsfdDatabaseModelConfigService;
import com.comac.ins.isfd.service.IIsfdDatabaseMongoService;
import com.comac.ins.isfd.service.IsfdServerManagementService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import static cn.dev33.satoken.SaManager.log;

/**
 *
 * @date: 2025/3/31 10:50
 **/
@RequiredArgsConstructor
@Service
public class IsfdServerManagementServiceImpl implements IsfdServerManagementService {
    @Autowired
    private IsfdServerManagementMapper isfdServerManagementMapper;
    @Autowired
    private final IsfdDatabaseModelConfigMapper isfdDatabaseModelConfigMapper;
    @Autowired
    private IsfdBusinessScenariosMapper isfdBusinessScenariosMapper;
    @Autowired
    private IsfdDatabaseTypeManagementMapper isfdDatabaseTypeManagementMapper;
    @Autowired
    IsfdDatabaseDataManagementServiceImpl isfdDatabaseDataManagementService;
    @Autowired
    private IIsfdDatabaseModelConfigService configService;
    @Autowired
    private IIsfdDatabaseMongoService mongoService;
    @Autowired
    private IIsfdDatabaseDataImportRecordService importRecordService;
    @Override
    public Boolean add(HttpServletRequest request,IsfdServerManagementAddBo isfdServerManagementAddBo) {
        IsfdSeverManagement add = MapstructUtils.convert(isfdServerManagementAddBo, IsfdSeverManagement.class);
        LoginUser loginUser = LoginHelper.getLoginUser();
        add.setCreateBy(loginUser.getUsername());
        add.setUpdateBy(loginUser.getUsername());
        add.setServiceTimes(0L);
        // 生成服务地址
        String host = "";
        try {
            host = request.getHeader("Host");
            if(!host.contains("8080")){
                host = host + ":8080";
            }
        } catch (Exception e) {
            throw new RuntimeException("获取本地IP地址失败");
        }
        if(StringUtils.isNotBlank(host) ){
            String addType = isfdServerManagementAddBo.getServerType()==1?"import":"export";
            String serverAddress = "";
            long timestamp = System.currentTimeMillis();
            if(addType.equals("export")){
                String condition = "?createStartTime=yyyy-mm-dd&createEndtTime=yyyy-mm-dd&updateStartTime=yyyy-mm-dd&updateEndTime=yyyy-mm-dd&pageSize=10&pageNum=1";
                serverAddress = "method:get;url:http://" + host + "/isfd/server/" + addType +"/" +timestamp+ "/" +add.getModelId()+condition;

            }else{
                serverAddress = "method:post;params:{datas:[]};url:http://" + host + "/isfd/server/" + addType +"/" +timestamp+ "/" +add.getModelId();
            }
            add.setServiceAddress(serverAddress);
        }
        return isfdServerManagementMapper.insert(add)>0;
    }

    @Override
    public Boolean delete(Long id) {
        IsfdSeverManagement isfdServerManagement = isfdServerManagementMapper.selectData(id);
        if(isfdServerManagement.getStatus().equals(1)){
            throw new BaseException("服务已发布，无法删除");
        }
        isfdServerManagementMapper.deleteData(id);
        return true;
    }

    @Override
    public Boolean update(IsfdServerManagementAddBo isfdServerManagementAddBo) {
        IsfdSeverManagement update = MapstructUtils.convert(isfdServerManagementAddBo, IsfdSeverManagement.class);
        LoginUser loginUser = LoginHelper.getLoginUser();
        update.setUpdateBy(loginUser.getUsername());
        return isfdServerManagementMapper.updateById(update)>0;
    }

    @Override
    public List<IsfdServerManagementVo> query(IsfdServerManagementQueryBo isfdServerManagementQueryBo) {
        int offset = (isfdServerManagementQueryBo.getPageNum() - 1) * isfdServerManagementQueryBo.getPageSize();
        List <IsfdServerManagementVo> result = isfdServerManagementMapper.queryList(isfdServerManagementQueryBo, isfdServerManagementQueryBo.getPageSize(), offset);
        // 查询中文名
        for (IsfdServerManagementVo isfdServerManagementVo : result) {
            IsfdBusinessScenarios isfdBusinessScenarios = new IsfdBusinessScenarios();
            IsfdBusinessScenarios isfdBusinessScenariosSystem = new IsfdBusinessScenarios();
            IsfdDatabaseModelConfig isfdDatabaseModelConfig = new IsfdDatabaseModelConfig();
            IsfdDatabaseTypeManagement isfdDataBase = new IsfdDatabaseTypeManagement();
            isfdBusinessScenarios = isfdBusinessScenariosMapper.selectById(isfdServerManagementVo.getSceneId());
            isfdBusinessScenariosSystem = isfdBusinessScenariosMapper.selectById(isfdServerManagementVo.getSystemId());
            isfdDatabaseModelConfig = isfdDatabaseModelConfigMapper.selectById(isfdServerManagementVo.getModelId());
            isfdDataBase = isfdDatabaseTypeManagementMapper.selectById(isfdServerManagementVo.getDatabaseTypeId());
            if(Objects.nonNull(isfdBusinessScenarios)){
                isfdServerManagementVo.setSceneName(isfdBusinessScenarios.getName());
            }
            if(Objects.nonNull(isfdBusinessScenariosSystem)){
                isfdServerManagementVo.setSystemName(isfdBusinessScenariosSystem.getName());
            }
            if(Objects.nonNull(isfdDatabaseModelConfig)){
                isfdServerManagementVo.setModelName(isfdDatabaseModelConfig.getModelName());
            }
            if(Objects.nonNull(isfdDataBase)){
                isfdServerManagementVo.setDatabaseTypeName(isfdDataBase.getDatabaseName());
            }
        }
        return result;

    }

    @Override
    public Long getTotal(IsfdServerManagementQueryBo isfdServerManagementQueryBo) {
        return isfdServerManagementMapper.getTotal(isfdServerManagementQueryBo);
    }

    @Override
    public IsfdServerDataImportVo serverDataImport(IsfdServerDataImportBo isfdServerDataImportBo) {
        IsfdServerDataImportVo isfdServerDataImportVo = new IsfdServerDataImportVo();
        List<?> datas = isfdServerDataImportBo.getDatas();
        List<IsfdServerDataImportVo.failDataObject> errorList = new ArrayList<>();
        Long successCount = 0L;
        Long failCount = 0L;
        IsfdDatabaseDataImportRecordBo recordBo = new IsfdDatabaseDataImportRecordBo();
        IsfdServerManagementVo isfdServerManagementVo = isfdServerManagementMapper.selectByUrl(isfdServerDataImportBo.getRequestUrl());
        //状态校验
        if(isfdServerManagementVo.getStatus().equals(0)){
            throw new BaseException("服务未发布，无法导入数据");
        }
        if(isfdServerManagementVo.getStatus().equals(2)){
            throw new BaseException("服务已撤销，无法导入数据");
        }
        if(datas.size()>0){
            isfdServerDataImportVo.setTotalCount(Integer.toUnsignedLong(datas.size()));
            IsfdDatabaseModelConfig po = isfdDatabaseModelConfigMapper.selectById(isfdServerDataImportBo.getModelId());
            if(po == null) {
                log.error("未找到关联模型！");
                throw new BaseException("未找到关联模型！");
            }

            recordBo.setModelId(po.getId());
            recordBo.setModelName(po.getModelName());
            recordBo.setImportMethod(IsfdImportMethodEnum.API.getDesc());

            // 检查数据
            @SuppressWarnings("unchecked")
            List<Map<String, Object>> dataList = (List<Map<String, Object>>) datas;
            IsfdDatabaseDataValidatedDataVo validatedDataVo = isfdDatabaseDataManagementService.checkImportDataList(po.getId(), null, dataList, null);

            int failedDataCount = validatedDataVo.getFailedDataCount();
            List<Map<String, String>> failedMessages = validatedDataVo.getFailedMessage();
            List<Map<String, List<Map<String, Object>>>> validatedDataList = validatedDataVo.getValidatedData();
            if (failedDataCount > 0) {
                for (Map<String, String> message : failedMessages) {
                    IsfdServerDataImportVo.failDataObject failDataObject = new IsfdServerDataImportVo.failDataObject();
                    failDataObject.setData(message.get("data"));
                    failDataObject.setErrorMessage("数据导入失败");
                    errorList.add(failDataObject);
                    failCount++;
                }
            }

            // 导入数据
            try{
                isfdDatabaseDataManagementService.importDataList(recordBo, validatedDataList);
                successCount = (long) dataList.size();
            }catch (Exception e){
                for (Map<String, Object> data : dataList) {
                    IsfdServerDataImportVo.failDataObject failDataObject = new IsfdServerDataImportVo.failDataObject();
                    failDataObject.setData(data);
                    failDataObject.setErrorMessage("数据导入失败");
                    errorList.add(failDataObject);
                    failCount++;
                }
            }
        }
        // 记录导入记录
        IsfdDatabaseModelConfig isfdDatabaseModelConfig = isfdDatabaseModelConfigMapper.selectById(isfdServerDataImportBo.getModelId());
        LoginUser loginUser = LoginHelper.getLoginUser();
        // 查询serverID
        if(Objects.isNull(isfdServerManagementVo)){
            throw new RuntimeException("服务模型不存在");
        }
        IsfdServerServiceHistroryVo isfdServerServiceHistroryVo = new IsfdServerServiceHistroryVo();
        isfdServerServiceHistroryVo.setServerId(isfdServerManagementVo.getId());
        isfdServerServiceHistroryVo.setDataCount(Integer.toUnsignedLong(datas.size()));
        isfdServerServiceHistroryVo.setTableName(isfdDatabaseModelConfig.getTableName());
        isfdServerServiceHistroryVo.setUpdateBy(loginUser.getUsername());
        isfdServerServiceHistroryVo.setCreateBy(loginUser.getUsername());
        isfdServerServiceHistroryVo.setUserName(loginUser.getUsername());
        isfdServerServiceHistroryVo.setRecordId(Objects.nonNull(recordBo.getId())? recordBo.getId():-1);
        //插入导入记录
        isfdServerManagementMapper.inserImportRecord(isfdServerServiceHistroryVo);
        // 更新调用次数
        isfdServerManagementMapper.updateCallCount(isfdServerManagementVo.getId(),isfdServerManagementVo.getServiceTimes()+1);
        // 更新导入记录表
        recordBo.setId(isfdServerServiceHistroryVo.getRecordId());
        recordBo.setDataQuantity(Integer.parseInt(String.valueOf(successCount)));
        recordBo.setSceneId(isfdServerManagementVo.getSceneId());
        recordBo.setSceneName(isfdServerManagementVo.getSceneName());
        recordBo.setSystemId(isfdServerManagementVo.getSystemId());
        recordBo.setSystemName(isfdServerManagementVo.getSystemName());
        recordBo.setModelId(isfdServerManagementVo.getModelId());
        recordBo.setModelName(isfdServerManagementVo.getModelName());
        recordBo.setTableName(isfdDatabaseModelConfig.getTableName());
        recordBo.setDatabaseTypeId(isfdServerManagementVo.getDatabaseTypeId());
        recordBo.setDatabaseTypeName(isfdServerManagementVo.getDatabaseTypeName());

        importRecordService.updateByBo(recordBo);
        // 返回数据
        isfdServerDataImportVo.setSuccessCount(successCount);
        isfdServerDataImportVo.setFailCount(failCount);
        isfdServerDataImportVo.setFailDatas(errorList);
        isfdServerDataImportVo.setTotalCount(successCount+failCount);
        return isfdServerDataImportVo;
    }

    @Override
    public IsfdServerDataExportVo serverDataExport(IsfdServerDataExportBo isfdServerDataExportBo) {
        IsfdServerManagementVo isfdServerManagementVo = isfdServerManagementMapper.selectByUrl(isfdServerDataExportBo.getRequestUrl());
        if(isfdServerManagementVo.getStatus().equals(0)){
            throw new BaseException("服务未发布，无法导出数据");
        }
        if(isfdServerManagementVo.getStatus().equals(2)){
            throw new BaseException("服务已撤销，无法导出数据");
        }
        IsfdDatabaseModelConfig po = isfdDatabaseModelConfigMapper.selectById(isfdServerDataExportBo.getModelId());
        //查询数据
        List<IsfdDatabaseMongoQuery.IsfdDatabaseMongoQueryCondition> conditions = new ArrayList<>();
        IsfdDatabaseMongoQuery queryCondition = new IsfdDatabaseMongoQuery();
        IsfdDatabaseMongoQuery.IsfdDatabaseMongoQueryCondition itemCondition = new IsfdDatabaseMongoQuery.IsfdDatabaseMongoQueryCondition();
        if(StringUtils.isNotBlank(isfdServerDataExportBo.getCreateTimeStart())){
            itemCondition.setFieldName("createTime");
            itemCondition.setValue(isfdServerDataExportBo.getCreateTimeStart());
            itemCondition.setLogicalOperator(IsfdMongoQueryLogicalOperatorEnum.AND.getValue());
            itemCondition.setOperator(IsfdMongoQueryOperatorEnum.GREATER_THAN_EQUALS.getValue());
            conditions.add(itemCondition);
        }
        if(StringUtils.isNotBlank(isfdServerDataExportBo.getCreateTimeEnd())){
            itemCondition.setFieldName("createTime");
            itemCondition.setValue(isfdServerDataExportBo.getCreateTimeEnd());
            itemCondition.setLogicalOperator(IsfdMongoQueryLogicalOperatorEnum.AND.getValue());
            itemCondition.setOperator(IsfdMongoQueryOperatorEnum.LESS_THAN_EQUALS.getValue());
            conditions.add(itemCondition);
        }
        if(StringUtils.isNotBlank(isfdServerDataExportBo.getUpdateStartTime())){
            itemCondition.setFieldName("updateTime");
            itemCondition.setValue(isfdServerDataExportBo.getUpdateStartTime());
            itemCondition.setLogicalOperator(IsfdMongoQueryLogicalOperatorEnum.AND.getValue());
            itemCondition.setOperator(IsfdMongoQueryOperatorEnum.LESS_THAN_EQUALS.getValue());
            conditions.add(itemCondition);
        }
        if(StringUtils.isNotBlank(isfdServerDataExportBo.getUpdateEndTime())){
            itemCondition.setFieldName("updateTime");
            itemCondition.setValue(isfdServerDataExportBo.getUpdateEndTime());
            itemCondition.setLogicalOperator(IsfdMongoQueryLogicalOperatorEnum.AND.getValue());
            itemCondition.setOperator(IsfdMongoQueryOperatorEnum.GREATER_THAN_EQUALS.getValue());
            conditions.add(itemCondition);
        }
        conditions.add(itemCondition);
        queryCondition.setConditions(conditions);
        Long tableDataCount = mongoService.count(po.getTableName());
        PageQuery pageQuery = new PageQuery();
        if(Objects.nonNull(isfdServerDataExportBo.getPageNum())) {
            pageQuery.setPageNum(isfdServerDataExportBo.getPageNum());
        }else{
            pageQuery.setPageNum(1);
        }
        if(Objects.nonNull(isfdServerDataExportBo.getPageSize())) {
            pageQuery.setPageSize(isfdServerDataExportBo.getPageSize());
        }else{
            pageQuery.setPageSize(tableDataCount.intValue());
        }
        queryCondition.setPageQuery(pageQuery);
        List<Document> documents = isfdDatabaseDataManagementService.queryDataByModelHasRecord(po.getId(),queryCondition);
        IsfdServerDataExportVo isfdServerDataExportVo = new IsfdServerDataExportVo();
        isfdServerDataExportVo.setDataList(documents);
        isfdServerDataExportVo.setTotal(Long.valueOf(documents.size()));
        // 记录导入记录
        LoginUser loginUser = LoginHelper.getLoginUser();
        IsfdServerServiceHistroryVo isfdServerServiceHistroryVo = new IsfdServerServiceHistroryVo();
        isfdServerServiceHistroryVo.setServerId(isfdServerManagementVo.getId());
        isfdServerServiceHistroryVo.setDataCount(Integer.toUnsignedLong(documents.size()));
        isfdServerServiceHistroryVo.setTableName(po.getTableName());
        isfdServerServiceHistroryVo.setUpdateBy(loginUser.getUsername());
        isfdServerServiceHistroryVo.setCreateBy(loginUser.getUsername());
        isfdServerServiceHistroryVo.setUserName(loginUser.getUsername());
        //插入调用记录
        isfdServerManagementMapper.inserImportRecord(isfdServerServiceHistroryVo);
        // 插入导出历史记录
        IsfdServerDataExportHistroryVo isfdServerDataExportHistroryVo = new IsfdServerDataExportHistroryVo();
        isfdServerDataExportHistroryVo.setServerId(isfdServerManagementVo.getId());
        isfdServerDataExportHistroryVo.setHistoryId(isfdServerServiceHistroryVo.getId());
        isfdServerDataExportHistroryVo.setUserName(loginUser.getUsername());
        isfdServerDataExportHistroryVo.setCreateBy(loginUser.getUsername());
        isfdServerDataExportHistroryVo.setUpdateBy(loginUser.getUsername());
        documents.forEach(document -> {
            Map dataMap = (Map) document;
            isfdServerDataExportHistroryVo.setDataId((String) dataMap.get("_id"));
            isfdServerManagementMapper.inserExportRecord(isfdServerDataExportHistroryVo);
        });
        // 更新调用次数
        isfdServerManagementMapper.updateCallCount(isfdServerManagementVo.getId(),isfdServerManagementVo.getServiceTimes()+1);
        return isfdServerDataExportVo;
    }

    @Override
    public IsfdServerServiceHistroryListVo queryServerHistory(IsfdServerServiceHistroryBo isfdServerServiceHistroryBo) {
        int pageSize = isfdServerServiceHistroryBo.getPageSize();
        int offset = (isfdServerServiceHistroryBo.getPageNum() - 1) * pageSize;
        List<IsfdServerServiceHistroryVo> dataList=isfdServerManagementMapper.queryServerHistory(isfdServerServiceHistroryBo.getId(), pageSize, offset);
        int total=isfdServerManagementMapper.queryServerHistoryTotal(isfdServerServiceHistroryBo.getId());
        IsfdServerServiceHistroryListVo isfdServerServiceHistroryListVo = new IsfdServerServiceHistroryListVo();
        isfdServerServiceHistroryListVo.setTotal(total);
        isfdServerServiceHistroryListVo.setRecord(dataList);
        return isfdServerServiceHistroryListVo;
    }

    @Override
    public IsfdServerServiceHistroryDataVo queryServerHistoryData(IsfdServerServiceHistroryBo isfdServerServiceHistroryBo) {
        IsfdServerServiceHistroryDataVo isfdServerServiceHistroryDataVo = new IsfdServerServiceHistroryDataVo();
        IsfdServerServiceHistroryVo isfdServerServiceHistroryVo = new IsfdServerServiceHistroryVo();
        isfdServerServiceHistroryVo = isfdServerManagementMapper.selectHisById(isfdServerServiceHistroryBo.getId());

        List<IsfdDatabaseMongoQuery.IsfdDatabaseMongoQueryCondition> conditions = new ArrayList<>();
        IsfdDatabaseMongoQuery queryCondition = new IsfdDatabaseMongoQuery();
        IsfdDatabaseMongoQuery.IsfdDatabaseMongoQueryCondition itemCondition = new IsfdDatabaseMongoQuery.IsfdDatabaseMongoQueryCondition();
        Long dataCount = 0L;
        IsfdDatabaseDataTreeQueryBo queryDataCondition = new IsfdDatabaseDataTreeQueryBo();
        //导入数据记录
        if(Objects.nonNull(isfdServerServiceHistroryVo.getRecordId())){
            itemCondition.setFieldName("importRecord.id");
            itemCondition.setValue(String.valueOf(isfdServerServiceHistroryVo.getRecordId()));
            itemCondition.setOperator(IsfdMongoQueryOperatorEnum.EQUAL.getValue());
            itemCondition.setValueNumberCheck(Boolean.TRUE);
            conditions.add(itemCondition);
            queryCondition.setConditions(conditions);
            List recordIds = new ArrayList<>();
            recordIds.add(isfdServerServiceHistroryVo.getRecordId());
            queryDataCondition.setImportRecordIds(recordIds);
            dataCount = mongoService.count(isfdServerServiceHistroryVo.getTableName(),queryCondition);
        }else{
            //导出数据记录
            //查询导出数据记录
            IsfdServerDataExportHistroryVo isfdServerDataExportHistroryVo = new IsfdServerDataExportHistroryVo();
            List<IsfdServerDataExportHistroryVo> isfdServerDataExportHistroryVos = isfdServerManagementMapper.selectExportRecordByHistoryId(isfdServerServiceHistroryVo.getId());
            List ids = isfdServerDataExportHistroryVos.stream().map(data-> data.getDataId()).collect(Collectors.toList());
            dataCount = Long.valueOf(isfdServerDataExportHistroryVos.size());
            queryDataCondition.setObjectIds(ids);
        }
        IsfdDatabaseModelConfigVo po = isfdDatabaseModelConfigMapper.selectByTableName(isfdServerServiceHistroryVo.getTableName());
        queryDataCondition.setModelId(po.getId());
        queryDataCondition.setTableName(po.getTableName());
        IsfdDatabaseDataTreeResultVo dataTreeResultVo = isfdDatabaseDataManagementService.queryDataTree(queryDataCondition);
        isfdServerServiceHistroryDataVo.setRecord(dataTreeResultVo.getContent());
        isfdServerServiceHistroryDataVo.setTotal(dataCount.intValue());
        return isfdServerServiceHistroryDataVo;
    }
    @Override
    public List queryServerHistoryDataAll(IsfdServerDataQueryBo isfdServerDataQueryBo) {
        //查询服务历史记录
        List<IsfdServerServiceHistroryVo> isfdServerServiceHistroryVoList = new ArrayList<>();
        isfdServerServiceHistroryVoList = isfdServerManagementMapper.selectHisByServerId(isfdServerDataQueryBo.getId());
        //组装查询条件
        IsfdDatabaseDataTreeQueryBo queryDataCondition = new IsfdDatabaseDataTreeQueryBo();
        if(Objects.nonNull(isfdServerServiceHistroryVoList)){
            queryDataCondition.setTableName(isfdServerServiceHistroryVoList.get(0).getTableName());
            queryDataCondition.setModelId(isfdServerDataQueryBo.getModelId());
            //判断服务类型(存储服务)
            if(isfdServerDataQueryBo.getServerType().equals(1)) {
                //导入数据
                List recordIds = new ArrayList<>();
                for (IsfdServerServiceHistroryVo isfdServerServiceHistroryVo : isfdServerServiceHistroryVoList) {
                    if (Objects.nonNull(isfdServerServiceHistroryVo.getRecordId())) {
                        recordIds.add(isfdServerServiceHistroryVo.getRecordId());
                    }
                }
                queryDataCondition.setImportRecordIds(recordIds);
            }else{
                //导出数据
                List<String> allids = new ArrayList<>();
                for (IsfdServerServiceHistroryVo isfdServerServiceHistroryVo : isfdServerServiceHistroryVoList) {
                    List<IsfdServerDataExportHistroryVo> isfdServerDataExportHistroryVos = isfdServerManagementMapper.selectExportRecordByHistoryId(isfdServerServiceHistroryVo.getId());
                    List<String> ids = isfdServerDataExportHistroryVos.stream().map(data-> String.valueOf(data.getDataId())).collect(Collectors.toList());
                    //循环ids
                    for (String id : ids) {
                        allids.add(id);
                    }
                }
                queryDataCondition.setObjectIds(allids);
            }
            //查询数据
            IsfdDatabaseDataTreeResultVo dataTreeResultVo = isfdDatabaseDataManagementService.queryDataTree(queryDataCondition);
            return dataTreeResultVo.getContent();
        }else{
            return new ArrayList<>();
        }
    }
    @Override
    public Boolean publish(Long id) {
        Boolean flag = true;
        try{
            isfdServerManagementMapper.publish(id);
            flag = true;
        }catch (Exception e){
            log.error("发布服务失败",e);
            flag = false;
            throw new RuntimeException("发布服务失败");
        }
        return flag;
    }

    @Override
    public Boolean revoke(Long id) {
        Boolean flag = true;
        try{
            isfdServerManagementMapper.revoke(id);
            flag = true;
        }catch (Exception e){
            log.error("撤销服务失败",e);
            flag = false;
            throw new RuntimeException("撤销服务失败");
        }
        return flag;
    }

    @Override
    public Integer getServiceTimesByModelId(Long modelId) {
        return isfdServerManagementMapper.getServiceTimesByModelId(modelId);
    }
}
